package linestrategy

import (
	"codejy/analysis/lineanalysis"
	"codejy/globalfunc"
	"codejy/store"
	"codejy/structdef"
	"strings"
)

// 函数返回值
type TOStrategy struct{}

// // 注册策略
func init() {
	RegisterStrategy(&TOStrategy{})
}

// 判断
func (f *TOStrategy) getId() int {
	return lineanalysis.To
}

func (f *TOStrategy) Execute(threadStore *structdef.ThreadStore) {
	content := threadStore.Code.LineContent
	//去掉to->
	content = content[2:]
	//判断是否有内容
	if len(content) == 0 {
		threadStore.ReturnFlag = true
		return
	}
	//计算表达式的值
	value := globalfunc.ExecuteFunc("AssignmentStrategyMode", threadStore, content).(string)
	//存储变量
	var variableType = globalfunc.ExecuteFunc("CodeAnalysis", threadStore, value).(int)
	//获取当前作用域(函数名称)
	currentenv := threadStore.CurrentEnv
	//截取函数名称获取@funcName@最后的位置
	index := strings.LastIndex(currentenv, "@funcName@")
	//截取函数名称
	funcName := currentenv[index+10:]
	//获取父级作用域
	parentCurrentEnv := threadStore.ParentEnv
	definitionValue := structdef.NewVarDefinitionValue(funcName, value, variableType)
	definitionValue.Extend = "func->to" //标记是函数返回值
	store.CreateAndModifyVariableScope(threadStore, parentCurrentEnv, definitionValue)
	//设置返回标志
	threadStore.ReturnFlag = true
}
